package com.ruoyi.information.service.back.impl;

import com.ruoyi.common.core.constant.SecurityConstants;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.information.domain.back.CloudClassroomBack;
import com.ruoyi.information.mapper.back.CloudClassroomBackMapper;
import com.ruoyi.information.mapper.back.RecomBackMapper;
import com.ruoyi.information.service.back.ICloudClassroomBackService;
import com.ruoyi.system.api.RemoteFileService;
import com.ruoyi.system.api.domain.PathVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 云课堂列表Service业务层处理
 *
 * @author ruoyi
 * @date 2023-04-28
 */
@Service
public class CloudClassroomBackServiceImpl implements ICloudClassroomBackService {
    @Autowired
    private CloudClassroomBackMapper cloudClassroomMapper;

    @Autowired
    private RemoteFileService remoteFileService;

    @Autowired
    private RecomBackMapper recomBackMapper;

    /**
     * 查询云课堂列表
     *
     * @param id 云课堂列表主键
     * @return 云课堂列表
     */
    @Override
    public CloudClassroomBack selectCloudClassroomById(Long id) {
        return cloudClassroomMapper.selectCloudClassroomById(id);
    }

    /**
     * 查询云课堂列表列表
     *
     * @param cloudClassroom 云课堂列表
     * @return 云课堂列表
     */
    @Override
    public List<CloudClassroomBack> selectCloudClassroomList(CloudClassroomBack cloudClassroom) {
        return cloudClassroomMapper.selectCloudClassroomList(cloudClassroom);
    }

    /**
     * 新增云课堂列表
     *
     * @param cloudClassroom 云课堂列表
     * @return 结果
     */
    @Override
    public int insertCloudClassroom(CloudClassroomBack cloudClassroom) {
        cloudClassroom.setCreateTime(DateUtils.getNowDate());
        return cloudClassroomMapper.insertCloudClassroom(cloudClassroom);
    }

    /**
     * 修改云课堂列表
     *
     * @param cloudClassroom 云课堂列表
     * @return 结果
     */
    @Override
    public int updateCloudClassroom(CloudClassroomBack cloudClassroom) {
        cloudClassroom.setUpdateTime(DateUtils.getNowDate());
        /*删除云资源*/
        if (cloudClassroom.getCover() != null) {
            CloudClassroomBack main = cloudClassroomMapper.selectCloudClassroomById(cloudClassroom.getId());
            if (main.getCover() != null) {
                comparePhoto(main.getCover(), cloudClassroom.getCover(), remoteFileService);
            }
        }
        return cloudClassroomMapper.updateCloudClassroom(cloudClassroom);
    }

    static void comparePhoto(String cover, String cover2, RemoteFileService remoteFileService) {
        List<String> photos = new ArrayList<>();
        /*旧资源*/
        String[] olds = cover.split(",");
        /*新资源*/
        String[] news = cover2.split(",");
        for (String old : olds) {
            int count = 0;
            for (String s : news) {
                if (s.equals(old)) {
                    count++;
                    break;
                }
            }
            if (count == 0) {
                photos.add(old);
            }
        }
        /*删除资源*/
        remoteFileService.delete(new PathVo(photos), SecurityConstants.FROM_SOURCE);

    }

    /**
     * 批量删除云课堂列表
     *
     * @param ids 需要删除的云课堂列表主键
     * @return 结果
     */
    @Override
    public int deleteCloudClassroomByIds(Long[] ids) {
        /*删除云资源*/
        List<String> photos = cloudClassroomMapper.selectCloudClassroomListPhotos(ids);
        if (photos != null) {
            remoteFileService.delete(new PathVo(photos), SecurityConstants.FROM_SOURCE);
        }
        recomBackMapper.deleteRecomByIdsAndType(ids,"云课堂");
        return cloudClassroomMapper.deleteCloudClassroomByIds(ids);
    }

    /**
     * 删除云课堂列表信息
     *
     * @param id 云课堂列表主键
     * @return 结果
     */
    @Override
    public int deleteCloudClassroomById(Long id) {
        return cloudClassroomMapper.deleteCloudClassroomById(id);
    }
}
